package minderupt.selenate.translate.javascript;

import minderupt.selenate.translate.ScriptBuilder;

/**
 *
 */
public class JavascriptBuilder implements ScriptBuilder {


    private static final String TEST_CASE_HEADER_START = "selenium.testCases[\"";
    private static final String TEST_CASE_HEADER_END = "\"] = function(selenium) {\n\n";
    private static final String TEST_STEP_COMMAND_START = "    selenium.";
    private static final String TEST_STEP_COMMAND_END = ";\n";
    private static final String TEST_CASE_END = "\n\n}\n\n";


    private StringBuilder script = new StringBuilder();



    @Override
    public void startNewTestCase(String testCaseName) {

        script.append(TEST_CASE_HEADER_START);
        script.append(testCaseName);
        script.append(TEST_CASE_HEADER_END);

    }

    @Override
    public void addSeleniumCommand(String command, String target, String value) {

        script.append(TEST_STEP_COMMAND_START);
        script.append(command);
        script.append("(");

        if(target != null) script.append("\"" + target + "\"");
        if(value != null) script.append(", \"" + value + "\"");

        script.append(")");
        script.append(TEST_STEP_COMMAND_END);

    }

    @Override
    public void endTestCase() {

        script.append(TEST_CASE_END);

    }

    @Override
    public String getScript() {
        return(this.script.toString());
    }
}
